package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;

/**
 * 菜单模块控制层
 * @author linzibo
 * @version 1.0
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    /**
     * 根据用户名查询菜单
     */
    @RequestMapping("/getMenu")
    @PreAuthorize("isAuthenticated()")
    public Result getMenu(String username) {
        try {
            List<Menu> menus = menuService.getMenu(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }
    /**
     * 分页查询菜单项
     * @param queryPageBean 分页信息
     */
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult data = menuService.findPage(
                    queryPageBean.getCurrentPage(),//当前页数
                    queryPageBean.getPageSize(), //每页条数
                    queryPageBean.getQueryString() //查询条件
            );
            return new Result(true, MessageConstant.QUERY_MENU_LIST_SUCCESS,data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_LIST_FAIL);
        }
    }

    /**
     * 根据id获得menu对象
     * @param id menu id
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    public Result findById(Integer id) {
        try {
            Menu menu =  menuService.findById(id);
            return new Result(true,MessageConstant.QUERY_MENU__SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU__FAIL);
        }
    }

    /**
     * 修改菜单项
     * @param menu 菜单信息
     */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU__FAIL);
        }
    }

    /**
     * 根据id删除菜单信息
     * @param id 菜单id
     */
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    public Result delete(Integer id) {
        try {
            menuService.delete(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        }catch(RuntimeException e){
            e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }

    /**
     * 添加菜单项
     */
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('MENU_ADD')")
    public Result add(@RequestBody Menu menu) {
        try {
            menuService.add(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }


    /**
     * 查询所有菜单数据
     */
    @RequestMapping(value = "/findAll")
    @PreAuthorize("hasAnyAuthority('ROLE_ADD','ROLE_EDIT')")
    public Result findAll(){
        List<Menu> data = null;
        try {
            data = menuService.findAll();
            return new Result(true,MessageConstant.QUERY_MENU_LIST_SUCCESS,data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_LIST_FAIL);
        }
    }

    /**
     * 根据角色id获得菜单ids
     */
    @RequestMapping("/findMenuIdsByRoleId")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result findMenuIdsByRoleId(Integer id){
        try {
            List<Integer> menuIdsByRoleId = menuService.findMenuIdsByRoleId(id);
            return new Result(true,MessageConstant.GET_MENUIDS_SUCCESS,menuIdsByRoleId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENUIDS_FAIL);
        }
    }
}
